/**
 * Created by gongxun on 16/11/22.
 */

var KwxTingUIWidget = GameBaseItem.extend({
    mPanelMain : null,
    Image_tingBg : null,
    Panel_card : null,
    Image_icon : null,
    Image_liangpai : null,
    mCards : null,
    mSeat : 0,
    mScale : 0.8,
    mHeight : 160,
    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/KwxTingUI.json");
    },

    setTingCards : function(cardNums){
        var count = cardNums.length;
        if (count > 0){
            this.setVisible(true);
        }else{
            this.setVisible(false);
            return;
        }

        if (this.mSeat == 0){
            var offset = 0;
            var cardScale = 54 / mo.kwxCardHelper.inhandCardSize.width;
            var cardSize = cc.size(mo.kwxCardHelper.inhandCardSize.width * cardScale, mo.kwxCardHelper.inhandCardSize.height * cardScale);


            for (var i = 0; i < this.mCards.length; i++){
                this.mCards[i].setVisible(false);
            }

            for (var i = 0; i < cardNums.length; i++){
                var card = this.getCard(i);
                card.setCardNum(cardNums[i]);
                card.setCardKind(0, 3);
                card.setScale(cardScale);
                card.setPosition(cc.p((cardSize.width + offset) * (i + 0.5), (90 - cardSize.height) / 2));
            }

            var bgSize = cc.size(60 + (cardSize.width + offset) * cardNums.length + 15, 90);
            this.Image_tingBg.setContentSize(bgSize);
            this.Panel_card.setPositionY(0);
            this.Image_liangpai.setPositionX(bgSize.width);
            this.Image_liangpai.setVisible(true);
            this.Image_icon.setPositionY(45);
            mo.seekWidgetByName(this.Image_tingBg, "Panel_leftCard").setVisible(false);
            this.setTingPos(bgSize);
        }else{
            var offset = 0;
            var cardScale = 52 / mo.kwxCardHelper.leftOpenCardSize.width;
            var cardSize = cc.size(mo.kwxCardHelper.leftOpenCardSize.width * cardScale, mo.kwxCardHelper.leftOpenCardSize.height * cardScale);
            var addY = 97 * cardScale;

            for (var i = 0; i < this.mCards.length; i++){
                this.mCards[i].setVisible(false);
            }

            for (var i = 0; i < cardNums.length; i++){
                var card = this.getCard(i);
                card.setCardNum(cardNums[i]);
                card.setCardKind(this.mSeat, 3, true);
                card.setScale(cardScale);
                var x = (60 - cardSize.width) / 2 + cardSize.width / 2;
                //20是牌的反差
                var y = -cardSize.height;
                card.setPosition(cc.p(x, -addY * i + y));
            }
            var bgSize = cc.size(60, 60 + cardSize.height + addY * (cardNums.length - 1) + 10);
            this.Image_tingBg.setContentSize(bgSize);
            this.Image_tingBg.setPositionY(-bgSize.height);
            this.Panel_card.setPositionY(bgSize.height - 60);
            this.Image_icon.setPositionY(bgSize.height - 30);
            this.setTingPos(bgSize);
        }
    },

    setMyTingCards : function(cardNums){
        var count = cardNums.length;
        if (count == 0){
            this.setVisible(false);
            return;
        }

        this.setTingCards(cardNums);

        //修改大小
        var bgSize = this.Image_tingBg.getContentSize();
        this.Image_tingBg.setContentSize(cc.size(bgSize.width, bgSize.height + 30));
        this.Panel_card.setPositionY(30);
        this.Image_liangpai.setVisible(false);
        this.Image_icon.setPositionY(60);

        //显示剩余张数
        var Panel_leftCard = mo.seekWidgetByName(this.Image_tingBg, "Panel_leftCard");
        Panel_leftCard.setVisible(true);
        for (var i = 0; i < 6; i++){
            var isShow = cardNums.length > i;
            var Text_leftCardNum = mo.seekWidgetByName(Panel_leftCard, "Text_leftCardNum" + i);
            Text_leftCardNum.setVisible(isShow);

            if (isShow){
                var cardNum = cardNums[i];
                var left = mo.kwxTableStatus.getLeftCardCount(cardNum);
                var card = this.getCard(i);
                var x = cc.rectGetMidX(card.getBoundingBox());
                Text_leftCardNum.setPositionX(x);
                mo.gameHelper.setTextString(Text_leftCardNum, "剩" + left + "张");
            }
        }
    },

    getCard : function(index){
        var result = null;
        if (index < this.mCards.length){
            result = this.mCards[index];
            result.setVisible(true);
        }else{
            result = new KwxCard();
            this.mCards.push(result);
            this.Panel_card.addChild(result);
        }

        return result;
    },

    setTingSeat : function(seat){
        this.mSeat = seat;

        if (seat == 0){
            this.mScale = 1;
            this.mPanelMain = mo.seekWidgetByName(this.ccsNode, "Panel_hor");
            mo.seekWidgetByName(this.ccsNode, "Panel_ver").setVisible(false);
        }else{
            this.mScale = 1;
            this.mPanelMain = mo.seekWidgetByName(this.ccsNode, "Panel_ver");
            mo.seekWidgetByName(this.ccsNode, "Panel_hor").setVisible(false);
        }

        this.setScale(this.mScale);

        //ui
        this.Image_tingBg = mo.seekWidgetByName(this.mPanelMain, "Image_tingBg");
        this.Panel_card = mo.seekWidgetByName(this.mPanelMain, "Panel_card");
        this.Image_icon = mo.seekWidgetByName(this.mPanelMain, "Image_icon");
        this.Image_liangpai = mo.seekWidgetByName(this.mPanelMain, "Image_liangpai");
        this.mCards = [];
    },

    setTingPos : function(bgSize){
        if (this.mSeat == 0){
            var position = cc.p(160, 150);
            this.setPosition(position);
        }else if (this.mSeat == 1){
            var position = cc.p(mo.gameSize.x - 160 - 60, mo.gameSize.y - 20);
            this.setPosition(position);
        }else if (this.mSeat == 2){
            var position = cc.p(160, mo.gameSize.y - 20);
            this.setPosition(position);
        }

        if (mo.gameMsgManager.kwxGameModel == 2 && this.mSeat != 0){
            var position = cc.p(120, mo.gameSize.y - 20);
            this.setPosition(position);
        }
    }
});